<?php
namespace Home\Controller;
use Think\Controller;
header("Content-type: text/html; charset=utf-8"); 
class InfoController extends CommonController {
    public function index(){
        $m = M("cate");
        $count=$m->field('ip_info.id,picurl,ip_name,cate_name')->join('ip_info ON ip_cate.id = ip_info.typeid')->count();
        $Page = new \Think\Page($count,8);
        $show = $Page->show();
        $list = $m->field('ip_info.id,picurl,ip_name,cate_name')->join('ip_info ON ip_cate.id = ip_info.typeid')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign("infoList",$list);
        // var_dump($list);
        $this->assign('page',$show);
    	$this->display();
    }
    public function edit(){
        $m = M("cate");
        $map['fid'] = array('gt',0);
        $typeList = $m->where($map)->select(); 
        $this->assign("typeList",$typeList);
        $m = M("info");
        $map['id'] = I('request.id','1');
        $info = $m->where($map)->select();
        // var_dump($info);
        $this->assign("info",$info);
        $this->display();
    }
    public function doedit(){
        $m = M("info");
        $data = $m->create();
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小 2M = 1024*1024*2=2097152 3M = 1024*1024*3=3145728
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =      './Public/img/info_ico/'; // 设置附件上传目录    // 上传文件
        $info   =   $upload->uploadOne($_FILES['pic']);
        if(!$info) {// 上传错误提示错误信息
          $this->error($upload->getError());
        }else{// 上传成功
          // $data['id'] = $_GET['id'];  
          $data['picurl']=$info['savepath'].$info['savename'];
          $data['intro']=$_POST['intro'];
          $data['ip_name']=$_POST['ip_name'];
          $data['typeid']=$_POST['typeid'];
          $data['phone']=$_POST['phone'];
          $data['price']=$_POST['price'];
          $id = $_GET['id'];
          $result = $m->where("id = {$id}")->save($data);      
          if($result>0){
              $this->success("修改成功",U('Info/index'),3);
          }else{
              $this->error("修改失败！");      
          }
        }   
    }


    public function delete(){
        $m=M("info");
        $id = $_GET['id'];
        $check = $m->where("id= {$id}")->find();
        $result = $m->delete($id);
        if($result>0){
            $this->success("删除成功！");
        }else{
            $this->error("删除失败！");
        }
    }
}